﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using blackhouse.BlackApart.Business.ApartmentServices;
using blackhouse.BlackApart.Business.Reservations;
using blackhouse.Currencies;
using blackhouse.JavaScript.JQuery;

namespace blackhouse.Aparts.Web.Modules {
    public partial class AdditionalServiceEditablePerRoom : System.Web.UI.UserControl, IReservationAdditionalServiceView {

        #region Fields

        private IWebReservation reservation;
        private IAdditionalService service;

        #endregion

        #region Properties

        public int Days { get; set; }
        public Currency Value { get; set; }
        public Currency UnitPrice { get; set; }

        #endregion

        #region Methods

        void IReservationAdditionalServiceView.Load(IWebReservation webReservation, IAdditionalService service) {
            this.reservation = webReservation;
            this.service = service;
            this.PopulateControls();
        }

        private void PopulateControls() {
            this.ltlLid.Text = this.service.Description;
            this.ltlApartmentLid.Text = this.service.ApartmentDescription;
            if (this.service.PriceOnDemand > 0)
                this.ltlPriceOnDemand.Value = this.service.PriceOnDemand;
            else
                this.pnlPriceOnDemand.Visible = false;
            this.ltlUnitPrice.Text = this.UnitPrice.ToString();
            this.chbOrder.Text = this.GetTextOnActivateService();
            int apCount = this.reservation.ApartmentReservation.Length;
            if (apCount > 1) {
                this.trCalc.Visible = true;
                this.ltlApartmentCount.Text = apCount.ToString();
            }
            if (!string.IsNullOrEmpty(this.service.ReviewTitle)) {
                this.lblReviewTitle.Text = this.service.ReviewTitle + ":";
                this.pnlClientReview.Visible = true;
            }
            this.AddJavaScript();
        }

        private string GetTextOnActivateService() {
            StringBuilder sb = new StringBuilder(HttpContext.GetGlobalResourceObject("Aparts", "orderService1").ToString());
            sb.Append(this.Value);
            sb.Append("</b>");
            return sb.ToString();
        }

        private void AddJavaScript() {
            JQueryLibraries jql = new JQueryLibraries();
            // jql.IncludeJQueryCheckboxes();
        }

        public void RegisterClientUI(Panel pnlLine) {
            this.chbOrder.Attributes.Add("ClientUI", pnlLine.ClientID);
        }

        public void ReceiveSave() {
            ReservationAdditionalService rService = this.service.AddToReservation(this.reservation);
            if (rService != null) {
                if (!string.IsNullOrEmpty(Request[this.chbOrder.UniqueID])) {
                    rService.BookService();
                    if (this.txtReview.Visible)
                        rService.ClientReview = Request[this.txtReview.UniqueID];
                    rService.Save();
                }
            }
        }

        #endregion

    }
}